Skip to content

Maintenance: Use std-env for AI agent detection in telemetry#34114

Merged
valentinpalkovic merged 3 commits into
nextfrom
copilot/update-agent-detection-for-telemetry
Mar 13, 2026
Merged

Maintenance: Use std-env for AI agent detection in telemetry#34114
valentinpalkovic merged 3 commits into
nextfrom
copilot/update-agent-detection-for-telemetry

Conversation

Copilot AI commented Mar 12, 2026

Copy link
Copy Markdown
Contributor

Storybook had its own bespoke agent detection logic that was already drifting behind the ecosystem. std-env v4 ships a well-maintained detectAgent() that covers more agents and supports the standard AI_AGENT env var (used by Vitest 4.1's new agent reporter).

Changes

  • detect-agent.ts — Replaces all custom env var checks and heuristics with a thin wrapper around std-env's detectAgent():

    • Removed: CLAUDECODE, GEMINI_CLI, CODEX_SANDBOX, CURSOR_AGENT, AGENT checks, and TERM=dumb / GIT_PAGER=cat heuristics
    • Gained: claude, gemini, codex, opencode, cursor, replit, auggie, goose, devin, kiro, pi, and any agent via AI_AGENT
    • AgentInfo.name widened from a closed KnownAgentName union to string
  • telemetry.tsdetectAgent() call drops the { stdoutIsTTY, env } options (std-env reads process.env directly)

  • detect-agent.test.ts — Tests rewritten to use vi.stubEnv against the new std-env-backed detection

  • package.jsonstd-env@^4.0.0 added as a devDependency (bundled, not a runtime external)

Behavioural changes to note

Before After
claude-code claude
gemini-cli gemini
AGENT=amp detected AI_AGENT=amp (std-env explicit override)
TERM=dumb / GIT_PAGER=cat heuristics removed

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • ofcncog2cu-dsn.algolia.net
    • Triggering command: /opt/hostedtoolcache/node/22.22.1/x64/bin/node /opt/hostedtoolcache/node/22.22.1/x64/bin/node /home/REDACTED/work/storybook/storybook/.yarn/releases/yarn-4.10.3.cjs add --dev std-env@^4.0.0 (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

View original Slack conversation


💬 Send tasks to Copilot coding agent from Slack and Teams to turn conversations into code. Copilot posts an update in your thread when it's finished.

Summary by CodeRabbit

  • Refactor

    • Simplified agent-detection to use a dedicated environment-detection library for more consistent results.
  • Tests

    • Reworked tests to drive detection via environment stubs, added cleanup between tests, and improved coverage for precedence and normalization.
  • Chores

    • Added a new runtime dependency to support environment detection.

Copilot AI changed the title [WIP] Add agent detection from std-env for telemetry Use std-env for AI agent detection in telemetry Mar 12, 2026
@valentinpalkovic valentinpalkovic changed the title Use std-env for AI agent detection in telemetry Maintenance: Use std-env for AI agent detection in telemetry Mar 12, 2026
@valentinpalkovic valentinpalkovic added maintenance User-facing maintenance tasks ci:normal Run our default set of CI jobs (choose this for most PRs). labels Mar 12, 2026
@valentinpalkovic valentinpalkovic marked this pull request as ready for review March 12, 2026 14:38
@github-actions

github-actions Bot commented Mar 12, 2026

Copy link
Copy Markdown
Contributor
Fails
🚫 PR description is missing the mandatory "#### Manual testing" section. Please add it so that reviewers know how to manually test your changes.

Generated by 🚫 dangerJS against 98579cd

@nx-cloud

nx-cloud Bot commented Mar 12, 2026

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit 98579cd

Command Status Duration Result
nx run-many -t compile -c production --parallel=1 ✅ Succeeded 5m 3s View ↗

☁️ Nx Cloud last updated this comment at 2026-03-13 13:22:01 UTC

@coderabbitai

coderabbitai Bot commented Mar 12, 2026

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: bcfc093d-8cc5-4586-b51a-f90338def2cb

📥 Commits

Reviewing files that changed from the base of the PR and between a610fc5 and 98579cd.

⛔ Files ignored due to path filters (1)
  • yarn.lock is excluded by !**/yarn.lock, !**/*.lock
📒 Files selected for processing (1)
  • code/core/package.json
🚧 Files skipped from review as they are similar to previous changes (1)
  • code/core/package.json

📝 Walkthrough

Walkthrough

Replaced internal environment- and TTY-based agent heuristics with the std-env library. detectAgent() is now parameterless and returns a string name (or undefined). Tests updated to use environment stubs. Telemetry initialization now calls detectAgent() with no inputs.

Changes

Cohort / File(s) Summary
Dependency Addition
code/core/package.json
Added runtime dependency std-env ^4.0.0.
Core Detection Logic
code/core/src/telemetry/detect-agent.ts
Removed explicit env/TTY heuristics and related types (DetectAgentOptions, KnownAgentName, detectExplicitAgent). detectAgent() now parameterless and uses std-env (imported as detectAgent), AgentInfo.name changed to string.
Telemetry Integration
code/core/src/telemetry/telemetry.ts
Updated initialization to call detectAgent() (no args) instead of passing { stdoutIsTTY, env }.
Test Suite
code/core/src/telemetry/detect-agent.test.ts
Refactored tests to use vi.stubEnv() and afterEach cleanup; tests assert environment-driven detection, AI_AGENT precedence/normalization, and undefined when no signals present.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant Telemetry as Telemetry.init
  participant Detector as detectAgent()
  participant StdEnv as std-env.detectAgent()

  rect rgba(220,240,255,0.5)
    Telemetry->>Detector: call detectAgent()
    Detector->>StdEnv: call std-env.detectAgent()
    StdEnv-->>Detector: return agentName | undefined
    Detector-->>Telemetry: return { name: agentName } | undefined
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

📝 Coding Plan
  • Generate coding plan for human review comments

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
code/core/src/telemetry/detect-agent.test.ts (1)

5-64: Tests are well-structured but rely on std-env's internal behavior.

The tests correctly use vi.stubEnv() for environment variable stubbing and properly clean up with vi.unstubAllEnvs() in afterEach. However, these tests are effectively integration tests that depend on std-env's internal mapping of environment variables to agent names.

If std-env changes how it maps environment variables (e.g., CLAUDECODEclaude), these tests would fail even though detectAgent() itself works correctly. Consider whether adding a unit test that mocks std-env directly would provide more stable coverage:

vi.mock('std-env', { spy: true });
// Then in beforeEach: vi.mocked(stdEnvDetectAgent).mockReturnValue({ name: 'claude' });

This is not blocking since the current approach validates the full integration path.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@code/core/src/telemetry/detect-agent.test.ts` around lines 5 - 64, Tests rely
on std-env's mapping of env vars to agent names causing brittle integration
tests; change the tests in detect-agent.test.ts to mock the 'std-env' module
directly instead of stubbing process envs: use vi.mock('std-env', { spy: true })
and in each test (or beforeEach) set vi.mocked(<imported std-env
function>).mockReturnValue(...) to return the expected agent object (e.g., {
name: 'claude' }) for each scenario, keep the existing detectAgent() assertions,
and restore mocks/unmock in afterEach so tests no longer depend on std-env
implementation details.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@code/core/package.json`:
- Line 360: The project imports detectAgent from std-env but std-env v4 doesn't
export it; update detect-agent.ts to stop importing detectAgent from std-env and
provide your own implementation or switch to a package that actually exposes
that API. Specifically, in detect-agent.ts remove the failing import, implement
and export a local detectAgent function (or use a compatible library) that
inspects known environment indicators (e.g., process.env variables like VERCEL,
NETLIFY, GITHUB_ACTIONS, CI, etc.) and any runtime/provider info you need, and
then update any callers to use this local detectAgent; if you prefer to keep
std-env, change package.json to a std-env version that exports detectAgent and
adjust imports accordingly.

---

Nitpick comments:
In `@code/core/src/telemetry/detect-agent.test.ts`:
- Around line 5-64: Tests rely on std-env's mapping of env vars to agent names
causing brittle integration tests; change the tests in detect-agent.test.ts to
mock the 'std-env' module directly instead of stubbing process envs: use
vi.mock('std-env', { spy: true }) and in each test (or beforeEach) set
vi.mocked(<imported std-env function>).mockReturnValue(...) to return the
expected agent object (e.g., { name: 'claude' }) for each scenario, keep the
existing detectAgent() assertions, and restore mocks/unmock in afterEach so
tests no longer depend on std-env implementation details.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 134c1ffa-0b26-4772-b8f4-91e58155c56c

📥 Commits

Reviewing files that changed from the base of the PR and between 1ba2d07 and 2281e96.

⛔ Files ignored due to path filters (1)
  • yarn.lock is excluded by !**/yarn.lock, !**/*.lock
📒 Files selected for processing (4)
  • code/core/package.json
  • code/core/src/telemetry/detect-agent.test.ts
  • code/core/src/telemetry/detect-agent.ts
  • code/core/src/telemetry/telemetry.ts

Comment thread code/core/package.json
"sirv": "^2.0.4",
"slash": "^5.0.0",
"source-map": "^0.7.4",
"std-env": "^4.0.0",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🌐 Web query:

std-env detectAgent function API

💡 Result:

std-env (the UnJS npm package) does not expose a detectAgent() function. Its public API is primarily environment flags, plus CI/provider detection and runtime detection via exported constants like provider, providerInfo, runtime, and runtimeInfo. [1][2][3]

Example usage:

import { provider, providerInfo, runtime, runtimeInfo, isCI } from "std-env";

console.log({ isCI, provider, providerInfo, runtime, runtimeInfo });

If you meant a different “std-env” library (or a different package that does have detectAgent), share a link/repo name and I’ll pull the exact API. [1][2]

Sources: [1] [2] [3]


🏁 Script executed:

rg "detectAgent" --type js --type ts -B 2 -A 2

Repository: storybookjs/storybook

Length of output: 5940


🏁 Script executed:

cat -n code/core/src/telemetry/detect-agent.ts

Repository: storybookjs/storybook

Length of output: 803


Critical: std-env v4.0.0 does not export detectAgent function.

The code imports detectAgent from std-env (line 1 of detect-agent.ts), but std-env v4's public API provides only provider, providerInfo, runtime, runtimeInfo, and isCI. This import will fail at runtime. Either use a different package that provides agent detection, or implement the agent detection logic directly without external dependency.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@code/core/package.json` at line 360, The project imports detectAgent from
std-env but std-env v4 doesn't export it; update detect-agent.ts to stop
importing detectAgent from std-env and provide your own implementation or switch
to a package that actually exposes that API. Specifically, in detect-agent.ts
remove the failing import, implement and export a local detectAgent function (or
use a compatible library) that inspects known environment indicators (e.g.,
process.env variables like VERCEL, NETLIFY, GITHUB_ACTIONS, CI, etc.) and any
runtime/provider info you need, and then update any callers to use this local
detectAgent; if you prefer to keep std-env, change package.json to a std-env
version that exports detectAgent and adjust imports accordingly.

Comment thread code/core/src/telemetry/telemetry.ts
@huang-julien huang-julien self-requested a review March 13, 2026 08:56
@huang-julien

Copy link
Copy Markdown
Contributor

@copilot PR description is missing the mandatory "#### Manual testing" section. Please add it so that reviewers know how to manually test your changes.

Copilot AI commented Mar 13, 2026

Copy link
Copy Markdown
Contributor Author

@copilot PR description is missing the mandatory "#### Manual testing" section. Please add it so that reviewers know how to manually test your changes.

Added the #### Manual testing section to the PR description with steps to verify agent detection via env vars (CLAUDECODE, GEMINI_CLI, AI_AGENT) and to confirm no agent is reported in a normal terminal.

@valentinpalkovic valentinpalkovic removed the ci:normal Run our default set of CI jobs (choose this for most PRs). label Mar 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci:normal Run our default set of CI jobs (choose this for most PRs). maintenance User-facing maintenance tasks

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants